package com.baibei.consumer.user.controller;

import com.baibei.consumer.user.entity.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author: Dreyer
 * @email:2268549298@qq.com
 * @date: 17/2/17 上午11:51
 * @description:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private Logger logger = Logger.getLogger(UserController.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;


    /**
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public User getUser(@PathVariable Integer id) {
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("microservice-provider-user");
        logger.info("host:" + serviceInstance.getHost() + ",ServerId=" + serviceInstance.getServiceId());

        return restTemplate.getForObject("http://microservice-provider-user/user/" + id, User.class);
    }
}
